/* Author: Nahuel F. Lema, Maxi Yañez

 */
var linksAmigosController = {
    defs: {
        container: $(js.get.item('#links-amigos-container .box-list')),
        tpl: js.escape.decode(js.get.item('#tpl')[0].innerHTML),
        items: false,
        imagePath: scriptsController.defs.baseUrl + 'content/links-amigos/'
    },

    wayPointsComponent: {
        defs: {
            load: false,
            container: $(js.get.item('#footer'))
        },

        load: function(){
            var componentsPath = scriptsController.defs.baseUrl + scriptsController.defs.componentsPath + 'waypoints/',
            self = this;

            $.getScript(componentsPath + 'waypoints.min.js', function(){
                self.defs.load = true;
            });
        }
    },

    paginate: {
        defs: {
            cantidad: 5,
            paginaActual: 0,
            hayMasResultados: true,
            infiniteScrollIniciado:false
        },

        get: function(){
            var self = this;
            scriptsController.loader.append($('#links-amigos-container header .wait'), 'tiny');
            $.ajax({
                type : 'POST',
                url  : 'controllers/paginar/linksAmigos',
                data : {
                    'cantidad' : self.defs.cantidad,
                    'pagina'   : self.defs.paginaActual
                },
                success: function (data) {
                    linksAmigosController.defs.items = data.linksAmigos;
                    self.defs.hayMasResultados = data.hayMasResultados;
                    linksAmigosController.print();
                    $.waypoints('refresh');
                    scriptsController.loader.remove($('#links-amigos-container header .wait'));
                    if (!self.defs.infiniteScrollIniciado) linksAmigosController.infiniteScroll();
                    self.defs.infiniteScrollIniciado = true;
                }
            });
        },

        init: function() {
            if (!!this.defs.hayMasResultados) {
                this.defs.paginaActual = this.defs.paginaActual+1;
                this.get();
            } else {
                linksAmigosController.wayPointsComponent.defs.container.waypoint('destroy');
                linksAmigosController = null;
            }

            if (!!linksAmigosController)
                if (!!!linksAmigosController.wayPointsComponent.defs.load) linksAmigosController.wayPointsComponent.load();
        }
    },

    infiniteScroll: function () {
        var self = this, opts = {
            offset: '100%'
        };

        linksAmigosController.wayPointsComponent.defs.container.waypoint(function(event, direction) {
            if (direction === 'down')
                self.paginate.init();
        },opts);
    },

    print: function(){
        var tpl = '', item, items = this.defs.items;

        if (!!items.length) {
            for (var x = 0, l = items.length; x < l; x++) {
                item = items[x];
                tpl += this.defs.tpl;
                tpl = tpl.replace(/\$\{id}/g, parseInt(item['id']))
                .replace(/\$\{nombre\}/g, item['nombre'])
                .replace(/\$\{contenido\}/g, item['contenido'])
                .replace(/\$\{imagen\}/g, item['imagen'])
                .replace(/\$\{url\}/g, item['url'])
                .replace(/\$\{imagePath\}/g, this.defs.imagePath)
                .replace(/\$\{[a-z]+\}/g, ''); //borra el patron en caso de que haya quedado alguno sin completar
            }
            this.defs.container.append(tpl);
        }

        tpl = item = items = x = l = null;
    },

    init: function() {
        this.paginate.init();
    }
}

linksAmigosController.init();